import { defineStore } from "pinia";
import type { ClassStore } from "./types/type";
import type { addClaResponseData, allClaResponseData, cla, deleteClaResponseData, pageClaResponseData, pageClass } from "@/api/class/type";
import { reqAddClass, reqAllClass, reqDeleteClass, reqFindById, reqPageClass, reqUpdateClass } from "@/api/class";

const useClassStore = defineStore("Class",{
    state:():ClassStore=>{
        return {
          classes:[]
        }
    },
    actions:{
        async addClass(data:cla){
          const result:addClaResponseData = await reqAddClass(data);
          if(result.status == 0){
            return result.data;
          }else{
            return Promise.reject(new Error(result.msg))
          }  
        },
        async findById(_id:string){
            const result:addClaResponseData = await reqFindById(_id);
            if(result.status == 0){
                return result.data as cla;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async updateClass(data:cla){
          const result:addClaResponseData = await reqUpdateClass(data);
          if(result.status == 0){
            return result.data;
          }else{
            return Promise.reject(new Error(result.msg))
          }  
        },
        async allClass(){
          const result:allClaResponseData = await reqAllClass();
          if(result.status == 0){
            this.classes = result.data as cla[];
            return result.data;
          }else{
            return Promise.reject(new Error(result.msg))
          }  
        },
        async pageClass(data:pageClass){
          const result:pageClaResponseData = await reqPageClass(data);
          if(result.status == 0){
            return result.data;
          }else{
            return Promise.reject(new Error(result.msg))
          }  
        },
        async deleteClass(_id:string){
          const result:deleteClaResponseData = await reqDeleteClass(_id);
          if(result.status == 0){
            return "ok";
          }else{
            return Promise.reject(new Error(result.msg))
          }  
        }
    }
})
export default useClassStore;